package com.tjsinfo.tjsoa.system.bo;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
import com.tjsinfo.tjsoa.system.dao.ITSystemMessageDAO;
import com.tjsinfo.tjsoa.system.vo.TSystemMessage;

public class SystemMessageBO implements ISystemMessageBO {
	private ITSystemMessageDAO systemMessageDAO;
	
	public ITSystemMessageDAO getSystemMessageDAO() {
		return systemMessageDAO;
	}

	public void setSystemMessageDAO(ITSystemMessageDAO systemMessageDAO) {
		this.systemMessageDAO = systemMessageDAO;
	}

	public Long addSystemMessage(TSystemMessage systemMessage) {
		// TODO Auto-generated method stub
		return  systemMessageDAO.save(systemMessage);
		
	}

	public  Boolean sendSystemMessage(TSystemMessage systemMessage) {
		// TODO Auto-generated method stub
		Properties props = new Properties();
	    ClassLoader cl = this.getClass().getClassLoader();
		InputStream is = cl.getResourceAsStream("com/tjsinfo/tjsoa/system/properties/systemResource.properties"); 
		try {
			props.load(is);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		try {
			Object []value=new Object[]{systemMessage};
			//参数类型
			Class par[]=new Class[]{TSystemMessage.class}; 
			Field[] fields = Class.forName("com.tjsinfo.tjsoa.system.vo.TSystemMessage").getDeclaredFields();
			for (Field field : fields) {
				if (field.getName().indexOf("is") == 0) {
					field.setAccessible(true);
					if ((Integer)field.get(systemMessage)== 1 ) {
						String classimpname = "system.message."+field.getName()+"imp";
						String classesname =  props.getProperty(classimpname); 
						Class c = Class.forName(classesname);
						Object o = c.newInstance();
						Method method = o.getClass().getDeclaredMethod("sendMessage", par);
						method.invoke(o,value);			
					}
				}				
			}
		   // addSystemMessage(systemMessage);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/*public static void main(String[] args) {
		TSystemMessage systemMessage = new TSystemMessage();
		systemMessage.setIsemail(1);
		try {
			Field[] fields = Class.forName("com.tjsinfo.tjsoa.system.vo.TSystemMessage").getDeclaredFields();
			for (Field field : fields) {
				if (field.getName().indexOf("is") == 0) {
					field.setAccessible(true);
					if ((Integer)field.get(systemMessage)== 1 ) {
					
					}
					
				}				
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}*/
}
